<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="!crud.props.searchToggle">
        <el-form size="small" label-width="70px">
          <el-row :gutter="10">
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="关键字">
                <el-input v-model="query.keyword" placeholder="输入关键字搜索" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="内部编码">
                <el-input v-model="query.skuId" placeholder="输入内部编码搜索" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="商家编码">
                <el-input v-model="query.iId" placeholder="输入商家编码搜索" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="商品名称">
                <el-input v-model="query.name" placeholder="输入商品名称搜索" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <rrOperation :crud="crud"/>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <crudOperation :permission="permission">
        <el-button
          slot="left"
          class="filter-item"
          size="mini"
          type="primary"
          @click="syncInventory"
        >
          同步库存
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          size="mini"
          type="primary"
          @click="syncHistoryInventory"
        >
          同步历史库存
        </el-button>
      </crudOperation>

      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="商品内部编码" prop="skuId">
            <el-input v-model="form.skuId" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="商家编码" prop="iId">
            <el-input v-model="form.iId" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="商品名称">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="可用数">
            <el-input v-model="form.usableQty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="主仓实际库存">
            <el-input v-model="form.qty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="订单占有数">
            <el-input v-model="form.orderLock" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="仓库待发数">
            <el-input v-model="form.pickLock" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="虚拟库存">
            <el-input v-model="form.virtualQty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="采购在途数">
            <el-input v-model="form.purchaseQty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="销退仓库存">
            <el-input v-model="form.returnQty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="进货仓库存">
            <el-input v-model="form.inQty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="次品库存">
            <el-input v-model="form.defectiveQty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="修改时间">
            <el-input v-model="form.modified" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="安全库存下限">
            <el-input v-model="form.minQty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="安全库存上限">
            <el-input v-model="form.maxQty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="库存锁定数">
            <el-input v-model="form.lockQty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="自定义仓1">
            <el-input v-model="form.customizeQty1" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="自定义仓2">
            <el-input v-model="form.customizeQty2" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="自定义仓3">
            <el-input v-model="form.customizeQty3" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" stripe border v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler" show-summary :summary-method="sumRow">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" width="55" label="序号"/>
        <el-table-column v-if="columns.visible('skuId')" prop="skuId" label="商品内部编码" width="100"/>
        <el-table-column v-if="columns.visible('iId')" prop="iId" label="商家编码" width="100"/>
        <el-table-column v-if="columns.visible('name')" prop="name" label="商品名称" width="100"/>
        <el-table-column v-if="columns.visible('usableQty')" prop="usableQty" label="可用数" width="100"/>
        <el-table-column v-if="columns.visible('qty')" prop="qty" label="主仓实际库存" width="100"/>
        <el-table-column v-if="columns.visible('orderLock')" prop="orderLock" label="订单占有数" width="100"/>
        <el-table-column v-if="columns.visible('pickLock')" prop="pickLock" label="仓库待发数" width="100"/>
        <el-table-column v-if="columns.visible('virtualQty')" prop="virtualQty" label="虚拟库存" width="100"/>
        <el-table-column v-if="columns.visible('purchaseQty')" prop="purchaseQty" label="采购在途数" width="100"/>
        <el-table-column v-if="columns.visible('returnQty')" prop="returnQty" label="销退仓库存" width="100"/>
        <el-table-column v-if="columns.visible('inQty')" prop="inQty" label="进货仓库存" width="100"/>
        <el-table-column v-if="columns.visible('defectiveQty')" prop="defectiveQty" label="次品库存" width="100"/>
        <el-table-column v-if="columns.visible('modified')" prop="modified" label="修改时间" width="100">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.modified) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('minQty')" prop="minQty" label="安全库存下限" width="100"/>
        <el-table-column v-if="columns.visible('maxQty')" prop="maxQty" label="安全库存上限" width="100"/>
        <el-table-column v-if="columns.visible('lockQty')" prop="lockQty" label="库存锁定数" width="100"/>
        <el-table-column v-if="columns.visible('customizeQty1')" prop="customizeQty1" label="自定义仓1" width="100"/>
        <el-table-column v-if="columns.visible('customizeQty2')" prop="customizeQty2" label="自定义仓2" width="100"/>
        <el-table-column v-if="columns.visible('customizeQty3')" prop="customizeQty3" label="自定义仓3" width="100"/>
        <el-table-column v-permission="['admin','jstInventory:edit','jstInventory:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudJstInventory from '@/api/erp/jstInventory'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

// crud交由presenter持有
const defaultCrud = CRUD({ title: '聚水潭库存记录', url: 'api/jstInventory', sort: 'id,desc', crudMethod: { ...crudJstInventory },
  optShow: {
    add: false,
    edit: false,
    del: false,
    download: false,
    leadingIn: false,
    enable: false,
    disable: false,
  },})
const defaultForm = { id: null, skuId: null, iId: null, name: null, usableQty: null, qty: null, orderLock: null, pickLock: null, virtualQty: null, purchaseQty: null, returnQty: null, inQty: null, defectiveQty: null, modified: null, minQty: null, maxQty: null, lockQty: null, customizeQty1: null, customizeQty2: null, customizeQty3: null }
export default {
  name: 'JstInventory',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {

      permission: {
        add: ['admin', 'jstInventory:add'],
        edit: ['admin', 'jstInventory:edit'],
        del: ['admin', 'jstInventory:del'],
        import: ['admin', 'jstInventory:import'],
        export: ['admin', 'jstInventory:export'],
        copy: ['admin', 'jstInventory:copy'],
        upload: ['admin', 'jstInventory:upload'],
        download: ['admin', 'jstInventory:download'],
      },
      rules: {
        skuId: [
          { required: true, message: '商品内部编码不能为空', trigger: 'blur' }
        ]
      }    }
  },
  watch: {
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },
    sumRow({ columns, data }){
      const sums = []
      for (let i in  columns) {
        if (['usableQty','qty','orderLock','pickLock','virtualQty',
          'purchaseQty','returnQty','inQty','defectiveQty','lockQty'].includes(columns[i].property)) {
          let selectSum = 0
          this.$refs.table.selection.forEach(item => {
            selectSum += item[columns[i].property]
          })
          sums[i] = <p>{selectSum}
          <hr/>
          {this.crud.summary[columns[i].property] || 0}</p>
        }
        sums[0] = <p>选中
        <hr/>
        合计</p>
      }
      return sums
    },
    //同步库存
    syncInventory(){
      crudJstInventory.syncInventory({i_ids:this.crud.selections.map(item=>item.iId).join(',')}).then(res=>{
        if (res.status === 200){
          this.$message.success('操作成功')
          this.crud.refresh()
        }else {
          this.$message.error(res.msg)
        }
      }).catch(err=>{
        that.crud.loadingClose()
      })
    },
    //同步历史库存
    syncHistoryInventory(){
      crudJstInventory.syncHistoryInventory({i_ids:this.crud.selections.map(item=>item.iId).join(',')}).then(res=>{
        if (res.status === 200){
          this.$message.success('操作成功')
          this.crud.refresh()
        }else {
          this.$message.error(res.msg)
        }
      }).catch(err=>{
        that.crud.loadingClose()
      })
    },
  }
}
</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
